from datetime import datetime
from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
from app import db, login_manager

class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), unique=True, index=True, nullable=False)
    email = db.Column(db.String(120), unique=True, index=True, nullable=False)
    password_hash = db.Column(db.String(128))
    role = db.Column(db.String(20), nullable=False)  # student, teacher, enterprise, admin
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    is_active = db.Column(db.Boolean, default=True)
    
    __mapper_args__ = {
        'polymorphic_identity': 'user',
        'polymorphic_on': role
    }
    
    def set_password(self, password):
        self.password_hash = generate_password_hash(password)
    
    def check_password(self, password):
        return check_password_hash(self.password_hash, password)
    
    def __repr__(self):
        return f'<User {self.username}>'

class Student(User):
    id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
    full_name = db.Column(db.String(100), nullable=False)
    student_id = db.Column(db.String(20), unique=True, nullable=False)
    major = db.Column(db.String(100), nullable=False)
    grade = db.Column(db.String(20))
    phone = db.Column(db.String(20))
    resume = db.Column(db.Text)  # 简历内容或文件路径
    
    __mapper_args__ = {
        'polymorphic_identity': 'student',
    }
    
    # 关系
    applications = db.relationship('Application', backref='applicant', lazy='dynamic')
    submissions = db.relationship('Submission', backref='student', lazy='dynamic')
    
    def __repr__(self):
        return f'<Student {self.full_name}>'

class Teacher(User):
    id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
    full_name = db.Column(db.String(100), nullable=False)
    department = db.Column(db.String(100), nullable=False)
    title = db.Column(db.String(50))
    
    __mapper_args__ = {
        'polymorphic_identity': 'teacher',
    }
    
    # 关系 - 指导的学生
    supervised_students = db.relationship('Student', secondary='supervision', 
                                         backref=db.backref('supervisors', lazy='dynamic'))
    
    def __repr__(self):
        return f'<Teacher {self.full_name}>'

class Enterprise(User):
    id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
    company_name = db.Column(db.String(100), nullable=False)
    address = db.Column(db.String(200))
    contact_person = db.Column(db.String(100), nullable=False)
    phone = db.Column(db.String(20), nullable=False)
    description = db.Column(db.Text)
    status = db.Column(db.String(20), default='pending')  # pending, approved, rejected
    
    __mapper_args__ = {
        'polymorphic_identity': 'enterprise',
    }
    
    # 关系
    internship_posts = db.relationship('InternshipPost', backref='company', lazy='dynamic')
    
    def __repr__(self):
        return f'<Enterprise {self.company_name}>'

class Admin(User):
    id = db.Column(db.Integer, db.ForeignKey('user.id'), primary_key=True)
    
    __mapper_args__ = {
        'polymorphic_identity': 'admin',
    }
    
    def __repr__(self):
        return f'<Admin {self.username}>'

# 关联表 - 教师指导学生
supervision = db.Table('supervision',
    db.Column('teacher_id', db.Integer, db.ForeignKey('teacher.id'), primary_key=True),
    db.Column('student_id', db.Integer, db.ForeignKey('student.id'), primary_key=True),
    db.Column('assigned_at', db.DateTime, default=datetime.utcnow)
)

class InternshipPost(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200), nullable=False)
    description = db.Column(db.Text, nullable=False)
    requirements = db.Column(db.Text)
    location = db.Column(db.String(100))
    duration = db.Column(db.String(50))  # 实习时长
    salary = db.Column(db.String(50))
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    is_active = db.Column(db.Boolean, default=True)
    
    # 外键
    enterprise_id = db.Column(db.Integer, db.ForeignKey('enterprise.id'), nullable=False)
    
    # 关系
    applications = db.relationship('Application', backref='internship_post', lazy='dynamic')
    
    def __repr__(self):
        return f'<InternshipPost {self.title}>'

class Application(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    status = db.Column(db.String(20), default='pending')  # pending, accepted, rejected
    applied_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
    cover_letter = db.Column(db.Text)
    
    # 外键
    student_id = db.Column(db.Integer, db.ForeignKey('student.id'), nullable=False)
    post_id = db.Column(db.Integer, db.ForeignKey('internship_post.id'), nullable=False)
    
    def __repr__(self):
        return f'<Application {self.id} - {self.status}>'

class Submission(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    type = db.Column(db.String(50), nullable=False)  # weekly_report, final_report, achievement
    title = db.Column(db.String(200), nullable=False)
    content = db.Column(db.Text)
    file_path = db.Column(db.String(300))  # 上传文件的路径
    submitted_at = db.Column(db.DateTime, default=datetime.utcnow)
    grade = db.Column(db.String(10))  # 成绩
    feedback = db.Column(db.Text)  # 教师反馈
    
    # 外键
    student_id = db.Column(db.Integer, db.ForeignKey('student.id'), nullable=False)
    
    def __repr__(self):
        return f'<Submission {self.type} - {self.title}>'

@login_manager.user_loader
def load_user(id):
    return User.query.get(int(id))